package main

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"note.zhiwangyunchuang.com/common"
	_ "note.zhiwangyunchuang.com/docs"
	"note.zhiwangyunchuang.com/network"
	"os"
)

func main() {
	// 初始化配置
	InitConfig()
	db := common.InitDb()
	defer db.Close()

	ginService := gin.Default()
	//TODO see(https://www.sohu.com/a/416697816_120750279)
	ginService.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	ginService.NoRoute(func(ctx *gin.Context) {
		network.Fail(ctx, "访问的路由不存在", gin.H{})
		return
	})

	// 初始化路由
	ginService = CollectRoute(ginService)

	ginService.Run(":8081")

}

func InitConfig() {
	workDir, _ := os.Getwd()
	viper.SetConfigName("application")
	viper.SetConfigType("yml")
	viper.AddConfigPath(workDir + "/config")
	err := viper.ReadInConfig()
	if err != nil {
		panic("配置读取失败：" + err.Error())
	}
}
